import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '../views/HomeView.vue'
import { storage } from '../utils/storage'
import { close, start } from '../utils/nprogress'


const routes: Array<RouteRecordRaw> = [
  {
    //isShow是否需要渲染为菜单，requireAuth是否有权限
    path: '/login',
    name: 'login',
    meta: { title: "登录系统", requireAuth: true, isShow: false },
    component: () => import(/* webpackChunkName: "login" */ '../views/LoginView.vue')
  },
  {
    path: '/',
    name: 'main',
    meta: { title: "后台首页", requireAuth: true, isShow: true },
    component: Layout,
    redirect: '/about',
    children: [
      {
        path: '/about',
        name: 'about',
        meta: { title: "后台首页", requireAuth: true, isShow: true },
        component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
      },
    ]
  },
  {
    path: '/admin',
    name: 'admin',
    meta: { title: "账号管理", requireAuth: true, isShow: true },
    component: Layout,
    children: [
      {
        path: '/adminList',
        name: 'adminList',
        meta: { title: "账号列表", requireAuth: true, isShow: true },
        component: () => import(/* webpackChunkName: "list" */ '../views/admin/AdminList.vue')
      },
      {
        path: '/adminType',
        name: 'adminType',
        meta: { title: "账号类型", requireAuth: true, isShow: true },
        component: () => import(/* webpackChunkName: "type" */ '../views/admin/AdminType.vue')
      }
    ]
  },
  {
    path: '/brand',
    name: 'brand',
    meta: { title: "品牌管理", requireAuth: true, isShow: true },
    component: Layout,
    children: [
      {
        path: '/brand',
        name: 'brand',
        meta: {
          requireAuth: true,
          title: '品牌管理',
          isShow: true
        },
        component: () => import(/* webpackChunkName: "brand" */ '../views/brand/CenterBrand.vue')
      },
    ]
  },
  {
    path: '/goods',
    name: 'goods',
    meta: { title: "商品管理", requireAuth: true, isShow: true },
    component: Layout,
    children: [
      {
        path: '/goodsList',
        name: 'goodsList',
        meta: {
          isShow: true,
          title: '商品列表',
          requireAuth: true,
        },
        component: () => import(/* webpackChunkName: "list" */ '../views/goods/GoodsList.vue')
      },
      {
        path: '/goodsType',
        name: 'goodsType',
        meta: {
          isShow: true,
          title: '商品类型',
          requireAuth: true,
        },
        component: () => import(/* webpackChunkName: "type" */ '../views/goods/GoodsType.vue')
      },
    ]
  },
  {
    path: '/merchant',
    name: 'merchant',
    meta: { title: "商家管理", requireAuth: true, isShow: true },
    component: Layout,
    children: [
      {
        path: '/merchantList',
        name: 'merchantList',
        meta: {
          isShow: true,
          title: '商家列表',
          requireAuth: true,
        },
        component: () => import(/* webpackChunkName: "list" */ '../views/merchant/MerchantList.vue')
      },
    ]
  },
  {
    path: '/order',
    name: 'order',
    meta: { title: "订单管理", requireAuth: true, isShow: true },
    component: Layout,
    children: [
      {
        path: '/orderList',
        name: 'orderList',
        meta: {
          isShow: true,
          title: '订单列表',
          requireAuth: true,
        },
        component: () => import(/* webpackChunkName: "list" */ '../views/order/OrderList.vue')
      },
      {
        path: '/orderContent',
        name: 'orderContent',
        meta: {
          isShow: true,
          requireAuth: true,
          title: '订单详情',
        },
        component: () => import(/* webpackChunkName: "content" */ '../views/order/OrderContent.vue')
      },
    ]
  },
  {
    path: '/PayType',
    name: 'PayType',
    meta: { title: "支付方式管理", requireAuth: true, isShow: true },
    component: Layout,
    children: [
      {
        path: '/PayType',
        name: 'PayType',
        meta: {
          isShow: true,
          title: '支付方式管理',
          requireAuth: true,
        },
        component: () => import(/* webpackChunkName: "brand" */ '../views/PayType/PayType.vue')
      }

    ]
  },

]


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


// 全局前置路由守卫路由守卫
router.beforeEach((to, from, next) => {

  if (to.meta.isAuth) {
    const token: string | null | undefined = storage.get('token')
    if (!token && to.path != '/login') {
      next('/login')
    } else {
      start()
      next()
    }
  } else {
    start()
    next()
  }
})

router.afterEach(() => {
  close()
})

export default router
